package com.lg.concurrent.schedule;

import lombok.SneakyThrows;

/**
 * wait、notify 是每个对象中的方法，在调用这些方法的时候必须先获取这个对象的锁
 *
 * notifyAll    唤醒的所用线程都在等待得到锁
 * notify       争抢了一次后 没有抢到后就又开始等待(wait)了
 *
 */
public class NotifyAllThread {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        WaitT waitT1 = new WaitT(lock);
        WaitT waitT2 = new WaitT(lock);
        WaitT waitT3 = new WaitT(lock);
        waitT1.start();
        waitT2.start();
        waitT3.start();

        Thread.sleep(1000);
//        new NotifyAllC(lock).notifyAllFun();
        new NotifyAllC(lock).notifyFun();

        waitT1.join();
        waitT2.join();
        waitT3.join();

    }

}

class  NotifyAllC{
    Object lock;
    NotifyAllC(Object lock) {
        this.lock = lock;
    }
     void  notifyFun(){
        synchronized (lock){
            lock.notify();
            System.out.println("notifyFun -- end");
        }
    }

    void  notifyAllFun(){
        synchronized (lock){
            lock.notifyAll();
            System.out.println("notifyAllFun -- end");
        }
    }
}

class WaitT extends Thread{
    Object lock;
    WaitT(Object lock){
        this.lock = lock;
    }
    @SneakyThrows
    @Override
    public void run() {
       synchronized (lock) {
            while (true) {
                lock.wait();
                System.out.println(Thread.currentThread().getName()+" --> end");
                break;
            }
        }
    }
}
